#!/bin/bash
# 一键部署所有数据库服务脚本

set -Eeuo pipefail

# 日志函数
log() { echo -e "$(date +'%F %T') | $*"; }
warn() { echo -e "$(date +'%F %T') | WARNING: $*" >&2; }
err() { echo -e "$(date +'%F %T') | ERROR: $*" >&2; }
trap 'err "脚本出错（行号: $LINENO）"' ERR

log "=========================================="
log "一键部署所有数据库服务"
log "=========================================="

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 检查 kubectl 是否可用
if ! command -v kubectl &> /dev/null; then
    err "kubectl 未安装或不在 PATH 中"
    exit 1
fi
log "✓ kubectl 可用"

# 1. 准备存储
log "[1/4] 准备存储..."
bash $SCRIPT_DIR/prepare-storage.sh
log "✓ 存储准备完成"

# 2. 部署 MySQL
log ""
log "[2/4] 部署 MySQL..."
bash $SCRIPT_DIR/deploy-mysql.sh
log "✓ MySQL 部署完成"

# 3. 部署 PostgreSQL
log ""
log "[3/4] 部署 PostgreSQL..."
bash $SCRIPT_DIR/deploy-postgresql.sh
log "✓ PostgreSQL 部署完成"

# 4. 部署 Redis
log ""
log "[4/4] 部署 Redis..."
bash $SCRIPT_DIR/deploy-redis.sh
log "✓ Redis 部署完成"

# 5. 验证所有服务
log ""
log "=========================================="
log "验证所有服务..."
log "=========================================="
bash $SCRIPT_DIR/verify-all.sh

log ""
log "=========================================="
log "所有数据库服务部署完成！"
log "=========================================="

